﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.ServiceModel;

namespace BluePortal.Framework
{
    public class Factory
    {
        public static T Create<T>(string fullClassname)
        {
            if (fullClassname == null) return Create<T>();
            Type realtype = Type.GetType(fullClassname);
            Object o = Activator.CreateInstance(realtype);
            return (T)o;
            //return PolicyInjection.Wrap<T>(o);            
        }

        public static T Create<T>()
        {
            string interfaceTypename = typeof(T).Name;
            string fullClassname = Properties.Settings.Default[interfaceTypename] as string;
            if (fullClassname == null) throw new Exception("Factory -- No configuration for type " + interfaceTypename);
            return Create<T>(fullClassname);
        }

        public static T CreateWCFProxy<T>()
        {
            try
            {
                string interfacename = typeof(T).FullName;
                ChannelFactory<T> factory = new ChannelFactory<T>(interfacename);
                return factory.CreateChannel();
            }
            catch (Exception ex)
            {
                Log.LogError("Factory", "CreateWCFProxy", ex);
                throw ex;
            }
        }
    }
}
